package com.hgyc.mom.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.entity.SysDict;
import com.hgyc.mom.common.service.SysDictService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.system.vo.SysDictParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 系统字典 前端控制器
 * </p>
 *
 * @author fwj
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/sys/dict")
@Tag(name = "字典接口", description = "字典接口")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @Operation(summary = "分页查询字典信息", description = "分页查询字典信息")
    public Result<IPage<SysDict>> list(
            @Parameter(description = "查询参数") @RequestBody SysDictParam sysDictParam) {
        Page<SysDict> page = new Page<>(sysDictParam.getPage(), sysDictParam.getPageSize());
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        if (sysDictParam.getDictCode() != null) {
            wrapper.like(SysDict::getDictCode, sysDictParam.getDictCode().trim());
        }
        if (sysDictParam.getDictName() != null) {
            wrapper.like(SysDict::getDictName, sysDictParam.getDictName());
        }
        IPage<SysDict> list = sysDictService.page(page, wrapper);
        return Result.success(list);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @Operation(summary = "获取所有字典", description = "获取所有字典")
    public Result<List<SysDict>> listAll() {
        return Result.success(sysDictService.list());
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public Result<SysDict> queryById(@PathVariable("id") Long id) {
        SysDict sysDict = sysDictService.getById(id);
        return Result.success(sysDict);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Operation(summary = "新增字典", description = "新增字典")
    public Result<?> add(
            @Parameter(description = "字典") @RequestBody SysDict sysDict) {
        sysDictService.addDict(sysDict);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Operation(summary = "更新字典", description = "更新字典")
    public Result<?> update(
            @Parameter(description = "字典") @RequestBody SysDict sysDict) {
        sysDictService.updateById(sysDict);
        return Result.success("更新成功");
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @Operation(summary = "批量删除字典项", description = "批量删除字典项")
    public Result<?> batchDel(
            @Parameter(description = "删除ID") @RequestParam("ids") List<Long> ids) {
        sysDictService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 字典生成枚举类代码
     */
    @GetMapping("/generateEnum/{dictId}")
    public Result<String> generateEnum(@PathVariable("dictId") Long dictId) throws IOException
    {
        String enumJavaCode = sysDictService.generatorEnum(dictId);
        return Result.success("代码生成成功",enumJavaCode);
    }
}
